<template>
	<main-container>
		<!-- 搜索 -->
		<el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch">
			<el-form-item label="优惠券名称">
				<el-input v-model="queryParams.couponNname" clearable></el-input>
			</el-form-item>
			<el-form-item label="状态">
				<el-select v-model="queryParams.status" clearable>
					<el-option label="正常" value="0"></el-option>
					<el-option label="禁用" value="1"></el-option>
				</el-select>
			</el-form-item>
			<el-form-item>
				<el-button type="primary" @click="initPage"
					><el-icon> <ele-Search /> </el-icon>搜索</el-button
				>
				<el-button @click="resetQuery"
					><el-icon> <ele-Refresh /> </el-icon>重置</el-button
				>
			</el-form-item>
		</el-form>
		<!-- 工具栏 -->
		<div class="hx-table-toolbar">
			<div></div>
			<right-toolbar :searchBtn="true" :refreshBtn="true" @search="showSearch = !showSearch" @refresh="initPage"></right-toolbar>
		</div>
		<!-- 列表 -->
		<el-table v-loading="loading" :data="tableData">
			<el-table-column prop="userInfoVO" label="用户信息" show-overflow-tooltip align="center">
				<template #default="scope">
					<span style="color: var(--el-color-primary)"> {{ scope.row.userInfoVO.nickName }}({{ scope.row.userInfoVO.phone }})</span>
				</template>
			</el-table-column>
			<el-table-column prop="couponName" label="优惠券" show-overflow-tooltip align="center"></el-table-column>
			<el-table-column prop="receivedTime" label="领取时间" align="center"></el-table-column>
			<el-table-column prop="validatTime" label="有效时间" align="center"></el-table-column>
			<el-table-column prop="usedTime" label="使用时间" align="center"></el-table-column>
			<el-table-column prop="status" label="状态" align="center">
				<template #default="scope">
					<el-tag v-if="scope.row.status == '0'" type="info">未使用</el-tag>
					<el-tag v-else-if="scope.row.status == '1'" type="danger">已使用</el-tag>
					<el-tag v-else-if="scope.row.status == '2'" type="danger">已过期</el-tag>
					<el-tag v-else-if="scope.row.status == '3'" type="danger">冻结</el-tag>
				</template>
			</el-table-column>
			<el-table-column prop="createTime" label="创建时间" width="180" />
		</el-table>
		<!-- 分页 -->
		<pagination
			v-model:total="page.total"
			v-model:currentPage="page.currentPage"
			v-model:pageSize="page.pageSize"
			@changePage="initPage"
		></pagination>
	</main-container>
</template>
<script lang="ts">
import { defineComponent, ref, reactive, toRefs } from 'vue';
import { getPage } from '/@/api/mall/couponuser';
export default defineComponent({
	setup() {
		const data = reactive({
			queryParams: {
				couponNname: '',
				status: '',
			},
			page: {
				total: 0,
				currentPage: 1,
				pageSize: 10,
				descs: 'create_time',
			},
			tableData: [],
		});
		const showSearch = ref(true);
		const loading = ref(false);

		const initPage = async () => {
			loading.value = true;
			let params = {
				current: data.page.currentPage,
				size: data.page.pageSize,
				descs: data.page.descs,
			};
			await getPage(Object.assign(params, data.queryParams))
				.then((response) => {
					data.tableData = response.data.data.records;
					data.page.total = response.data.data.total;
					loading.value = false;
				})
				.catch(() => {
					loading.value = false;
				});
		};
		/**
		 * 重置搜索表单
		 */
		const resetQuery = () => {
			data.queryParams = { couponNname: '', status: '' };
		};
		initPage();
		return {
			loading,
			showSearch,
			initPage,
			resetQuery,
			...toRefs(data),
		};
	},
});
</script>
